package com.fhz.kotlinstest.pagingSource

import android.os.Bundle
import android.util.Log
import android.view.ViewGroup
import androidx.activity.enableEdgeToEdge
import androidx.activity.viewModels
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.lifecycle.lifecycleScope
import androidx.paging.CombinedLoadStates
import androidx.paging.LoadState
import androidx.paging.PagingData
import androidx.recyclerview.widget.LinearLayoutManager
import com.fhz.kotlinstest.R
import com.fhz.kotlinstest.databinding.ActivityPag3Binding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.delay
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.launch

private const val TAG = "Pag3Activity"
@AndroidEntryPoint
class Pag3Activity : AppCompatActivity() , PagingPageState{

    val pag3ViewModel: Pag3ViewModel by viewModels()
    override val pageState: MutableStateFlow<PageState> = MutableStateFlow<PageState>(PageState.Loading)

    override val refreshDataAction = {

    }

    private var pageStateHost: PageStateHost? = null

    private lateinit var binding: ActivityPag3Binding
    private lateinit var pag3Adapter: Pag3Adapter
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityPag3Binding.inflate(layoutInflater)
        setContentView(binding.root)
        initPageState(binding.root)

        initAdapter()

        //在线上添加了个readme .解决冲突

    }

    private fun initAdapter() {
        binding.rv.layoutManager = LinearLayoutManager(this)
        pag3Adapter = Pag3Adapter()
        binding.rv.adapter = pag3Adapter

        //状态监听
        pag3Adapter.addLoadStateListener(loadStateListener)

        val items = pag3ViewModel.items


        lifecycleScope.launch {
            //网络里面
           // pag3Adapter.submitData(PagingData.from(listOf("1", "2", "3", "4", "5", "6", "7", "8", "9", "10")))
            delay(2000)
            items.collectLatest {
                pag3Adapter.submitData(it)
                Log.i(TAG, "initAdapter: ")
            }
            pageStateHost!!.setState(PageState.Empty)
        }
    }

    fun initPageState(viewGroup: ViewGroup) {
        if (this is PagingPageState) {
            pageStateHost = PageStateHost(this, refreshAction = refreshDataAction).apply {
                init(viewGroup)
                lifecycleScope.launch {
                    pageState.collectLatest {
                        Log.i(TAG, "initPageState: ")
                        setState(it)
                    }
                }
            }
        }
    }



    private val loadStateListener = object : Function1<CombinedLoadStates, Unit> {
        override fun invoke(combinedLoadStates: CombinedLoadStates) {
            when (val state = combinedLoadStates.refresh) {
                is LoadState.Loading -> {
                    lifecycleScope.launch {
                        pageState.emit(PageState.Loading)
                    }
                }
                is LoadState.Error -> {
                    // Show error message or handle error state
                    val errorMessage = state.error.message ?: "Unknown error"
                    // Display the error message to the user
                    lifecycleScope.launch {
                        pageState.emit(PageState.Error(Throwable(errorMessage)))
                    }
                }
                is LoadState.NotLoading -> {
                    // Hide loading animation or loading indicator
                    lifecycleScope.launch {
                        if (pag3Adapter.itemCount == 0) {
                            pageState.emit(PageState.Empty)
                        } else {
                            pageState.emit(PageState.NotLoading)
                        }
                    }

                }
            }
        }
    }


}
interface PagingPageState {
    val pageState: MutableStateFlow<PageState>
    val refreshDataAction: () -> Unit
}

